package com.example.cardstackproject.card

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.example.cardstackproject.R
import java.lang.ref.WeakReference


abstract class BaseOverlayPageAdapter(var context: Context, var mRequestOptions: RequestOptions) : PagerAdapter() {

    private var imgUrl:Array<String> = arrayOf()
    private var bitmaps : Array<WeakReference<Bitmap>> = arrayOf()

    abstract fun itemView(): View

    fun setImgUrlsAndBindViewPager(vp: ViewPager, imgUrl:Array<String>){
        setImgUrlsAndBindViewPager(vp,imgUrl,3)
    }

    fun setImgUrlsAndBindViewPager(vp: ViewPager, imgUrl: Array<String>, layerAmount:Int){
        setImgUrlsAndBindViewPager(vp,imgUrl,layerAmount,-1F,-1F)
    }

    /**
     * 显示层数
     */
    fun setImgUrlsAndBindViewPager(vp: ViewPager,imgUrl: Array<String>, layerAmount:Int,scaleOffset:Float,
                                   transOffset:Float) {
        this.imgUrl = imgUrl
        if (imgUrl.isNotEmpty()) {
            bitmaps = Array(imgUrl.size) {
                WeakReference(null)
            }
            vp.offscreenPageLimit = layerAmount
            val transformer = OverlayTransformer(layerAmount, scaleOffset, transOffset)
            vp.setPageTransformer(true, transformer)
        }
    }

    override fun getCount(): Int {
       if(imgUrl.isEmpty()){
           return  0
       }else if(imgUrl.size<=1){
           return imgUrl.size
       }
        return 20
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view==`object`
    }

    protected fun findImageView(rootView:View) : ImageView {
        val iv:ImageView =rootView.findViewById(R.id.card_iv)
        if(null!=iv)
            return iv
       if (rootView is ImageView){
          return itemView() as ImageView
       }
        throw RuntimeException("you should set one of ImageViews id=card_iv or rootView=ImageView")
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val index=position % imgUrl.size
        val imgUrl:String =imgUrl[index]
        val view : View =itemView()
        if (null == view){
            throw RuntimeException("You should set a item layout")
        }
        val iv:ImageView=findImageView(view)
        if (null == view){
            throw RuntimeException("You should set a item layout")
        }
        if (null!=bitmaps && null!=bitmaps[index] && null!=bitmaps[index].get()){
            iv.setImageBitmap(bitmaps[index].get())
        }
        Glide.with(context).asBitmap().load(imgUrl).apply(mRequestOptions).into(object :
            SimpleTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                bitmaps[index] = WeakReference<Bitmap>(resource)
                iv.setImageBitmap(resource)
            }

            override fun onLoadStarted(placeholder: Drawable?) {
                super.onLoadStarted(placeholder)
                iv.setImageDrawable(placeholder)
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                super.onLoadFailed(errorDrawable)
                iv.setImageDrawable(errorDrawable)
            }
        })
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }


}